home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
WASTE Object Handlers 1.2.6.sit
/
WASTE Object Handlers 1.2.6
/
Handler Source
/
WE_hfs_Handler.c
next >
Wrap
Text File
|
1997-09-08
|
9KB
|
327 lines
// File Object Handler for the WASTE Text Engine
// by Michael F. Kamprath, kamprath@kagi.com
// maintenance by John C. Daub, hsoi@eden.com
//
// v1.0, 12 March 1995
//
// v1.1, 1 April. Fixed a a bug with draging hard drives to a WASTE instance
// and a bix with insert file objects via InsertFileRefFromFSSpec()
//
// v1.2, 28 March 1996. Minor touch-ups and updates. Added more precompiler directives.
// Added compatability with WASTE 1.2a5.
//
// v1.2.2, 16 July 1996. Added compatability with WASTE 1.2 and CW9
// Restructured installation routines
//
// v1.2.6, 8 Sept 1996. Fixed bugs in HandlDrawHFS and SizeHFSObject where the Handle
// was being dereferenced without being locked.
#ifndef __ICONS__
#include <Icons.h>
#endif
#ifndef __DRAG__
#include <Drag.h>
#endif
#ifndef __LOWMEM__
#include <LowMem.h>
#endif
#ifndef _WASTE_
#include "WASTE.h"
#endif
#ifndef _WASTEOBJECTS_
#include "WASTE_Objects.h"
#endif
#include "WE_hfs_Handler.h"
#include "GetFileIcon.h"
#include "SendFinderOpen.h"
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
// Local Functions
static void SizeHFSObject( Point *objectSize, HFSFlavor **theHFS );
//
// InstallHFSObject()
// Installs the HFS Object handler into WASTE.
//
OSErr InstallHFSObject( WEReference theWE )
{
OSErr iErr;
#ifdef __cplusplus
static WENewObjectUPP newHFSUPP = NewWENewObjectProc(HandleNewHFS);
static WEDisposeObjectUPP disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
static WEDrawObjectUPP drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
static WEClickObjectUPP clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
#else
static WENewObjectUPP newHFSUPP = NULL;
static WEDisposeObjectUPP disposeHFSUPP = NULL;
static WEDrawObjectUPP drawHFSUPP = NULL;
static WEClickObjectUPP clickHFSUPP = NULL;
if ( newHFSUPP == NULL )
newHFSUPP = NewWENewObjectProc(HandleNewHFS);
if ( disposeHFSUPP == NULL )
disposeHFSUPP = NewWEDisposeObjectProc(HandleDisposeHFS);
if ( drawHFSUPP == NULL )
drawHFSUPP = NewWEDrawObjectProc(HandleDrawHFS);
if ( clickHFSUPP == NULL )
clickHFSUPP = NewWEClickObjectProc(HandleClickHFS);
#endif
if ( newHFSUPP != NULL )
iErr = WEInstallObjectHandler(flavorTypeHFS, weNewHandler, (UniversalProcPtr)newHFSUPP, theWE);
else
iErr = weUnknownObjectTypeErr;
if (iErr) return(iErr);
if ( disposeHFSUPP != NULL )
iErr = WEInstallObjectHandler(flavorTypeHFS, weDisposeHandler, (UniversalProcPtr)disposeHFSUPP, theWE);
else
iErr = weUnknownObjectTypeErr;
if (iErr) return(iErr);
if ( drawHFSUPP != NULL)
iErr = WEInstallObjectHandler(flavorTypeHFS, weDrawHandler, (UniversalProcPtr)drawHFSUPP, theWE);
else
iErr = weUnknownObjectTypeErr;
if (iErr) return(iErr);
if ( clickHFSUPP != NULL )
iErr = WEInstallObjectHandler(flavorTypeHFS, weClickHandler, (UniversalProcPtr)clickHFSUPP, theWE);
else
iErr = weUnknownObjectTypeErr;
if (iErr) return(iErr);
return(noErr);
}
//
// New Object Handler for HFS Objects
//
pascal OSErr HandleNewHFS(Point *defaultObjectSize,WEObjectReference objectRef)
{
HFSFlavor **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
// First size the object as WASTE requires
SizeHFSObject( defaultObjectSize, theHFS);
// Set the object refcon to 0. This is for safety
// as later we use the refcon to store the icon handle
// for this object.
WESetObjectRefCon( objectRef, 0 );
return(noErr);
}
//
// Dispose Object handler for HFS Objects
//
pascal OSErr HandleDisposeHFS(WEObjectReference objectRef )
{
Handle theHFS = WEGetObjectDataHandle(objectRef);
Handle iconCache = (Handle)WEGetObjectRefCon( objectRef );
// If the object has an icon handle loaded, dispose of it.
if (iconCache != 0)
{
DisposeIconSuite( iconCache, true );
}
// Dispos of the HFS data
DisposeHandle(theHFS);
return(MemError());
}
//
// Draw Object Handler for HFS objects
//
pascal OSErr HandleDrawHFS(Rect *destRect, WEObjectReference objectRef )
{
HFSFlavor **theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
Handle iconCache = (Handle)WEGetObjectRefCon( objectRef );
FontInfo fInfo;
GrafPtr thePort;
Rect iconRect;
OSErr iErr;
short oldTextFont, oldTextSize, oldTextFace;
short sysTextFont = LMGetSysFontFam();
short sysTextSize = LMGetSysFontSize();
char state;
// if the object's refcon did not contain an icon handle, load one.
if (iconCache == 0)
{
state = HGetState((Handle)theHFS);
HLockHi( (Handle)theHFS );
if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='fold') )
iErr = GetIconSuite(&iconCache,genericFolderIconResource,svAllAvailableData);
else if ( ((*theHFS)->fileCreator=='MACS')&&((*theHFS)->fileType=='disk') )
{
iErr = GetIconSuite(&iconCache,genericHardDiskIconResource,svAllAvailableData);
}
else
{
iErr = GetFileIcon( &(*theHFS)->fileSpec, svAllAvailableData, &iconCache );
if (iErr)
iErr = GetIconSuite(&iconCache,genericDocumentIconResource,svAllAvailableData);
}
HSetState((Handle)theHFS, state );
if (iErr)
return(iErr);
WESetObjectRefCon( objectRef, (long)iconCache );
}
// Determine the icon's rectangle and plot it
SetRect( &iconRect, (destRect->right + destRect->left)/2 - 16, destRect->top,
(destRect->right + destRect->left)/2 + 16, destRect->top + 32 );
iErr = PlotIconSuite(&iconRect,(IconAlignmentType)(atVerticalCenter + atHorizontalCenter),
(IconTransformType)ttNone, iconCache);
// Draw the file's title. First save old font info for port.
GetPort(&thePort);
oldTextFont = thePort->txFont;
oldTextSize = thePort->txSize;
oldTextFace = thePort->txFace;
TextFont( sysTextFont );
TextSize( sysTextSize );
TextFace( 0 );
GetFontInfo(&fInfo);
MoveTo( destRect->left + 1, destRect->bottom - fInfo.descent - fInfo.leading );
state = HGetState((Handle)theHFS);
HLock((Handle)theHFS);
DrawString( (*theHFS)->fileSpec.name );
HSetState((Handle)theHFS, state);
TextFont( oldTextFont );
TextSize( oldTextSize );
TextFace( oldTextFace );
return( iErr );
}
//
// Click Handler for HFS Objects.
// Will send the finder an open selection apple event when object is clicked.
//
pascal Boolean HandleClickHFS( Point hitPt,
short modifiers,
long clickTime,
WEObjectReference objectRef)
{
#pragma unused (hitPt, clickTime)
HFSFlavor **theHFS;
char state;
if (modifiers & 0x0001) // look for double-clicks
{
theHFS = (HFSFlavor**)WEGetObjectDataHandle(objectRef);
state = HGetState((Handle)theHFS);
HLockHi( (Handle)theHFS );
SendFinderOpenAE(&(*theHFS)->fileSpec);
HSetState((Handle)theHFS, state );
return(true);
}
else
return(false);
}
//
// SizeHFSObject()
// Used to return the display size of an HFS object
//
static void SizeHFSObject( Point *objectSize, HFSFlavor **theHFS )
{
FontInfo fInfo;
GrafPtr thePort;
short oldTextFont, oldTextSize, oldTextFace;
short strWidth, strHeight;
short sysTextFont = LMGetSysFontFam();
short sysTextSize = LMGetSysFontSize();
char state;
GetPort(&thePort);
oldTextFont = thePort->txFont;
oldTextSize = thePort->txSize;
oldTextFace = thePort->txFace;
TextFont( sysTextFont );
TextSize( sysTextSize );
TextFace( 0 );
state = HGetState((Handle)theHFS);
HLock((Handle)theHFS);
strWidth = StringWidth( (*theHFS)->fileSpec.name ) + 4;
HSetState((Handle)theHFS, state);
GetFontInfo(&fInfo);
strHeight = fInfo.ascent + fInfo.descent + fInfo.leading;
TextFont( oldTextFont );
TextSize( oldTextSize );
TextFace( oldTextFace );
objectSize->h = ( strWidth > 32 ) ? strWidth : 32 ;
objectSize->v = 32 + strHeight + 1;
}
//
// InsertFileRefFromFSSpec()
// Inserts a file object into a WASTE instance.
//
OSErr InsertFileRefFromFSSpec( FSSpec *theFile, WEReference theWE )
{
FInfo fndrInfo;
HFSFlavor **theHFS;
Point theSize;
OSErr iErr;
char state;
theHFS = (HFSFlavor**)NewHandleClear( sizeof(HFSFlavor) );
if (theHFS)
{
// First construct the HFSFlavor object from the FSSpec
iErr = FSpGetFInfo(theFile,&fndrInfo);
if (iErr)
{
DisposeHandle( (Handle)theHFS );
return(iErr);
}
state = HGetState((Handle)theHFS);
HLock( (Handle)theHFS );
(*theHFS)->fileType = fndrInfo.fdType;
(*theHFS)->fileCreator = fndrInfo.fdCreator;
(*theHFS)->fdFlags = fndrInfo.fdFlags;
(*theHFS)->fileSpec = (*theFile);
HSetState((Handle)theHFS, state );
SizeHFSObject( &theSize, theHFS );
// Now insert it into the WEReference
WEInsertObject( flavorTypeHFS, (Handle)theHFS, theSize, theWE );
}
return(noErr);
}